<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拖拽和颜色变化</title>
    <style>
        *{margin: 0;padding: 0;}
        #ccb div{margin-left: 330px;}
        #box{
            width: 100px;
            height: 100px;
            background-color: antiquewhite;
            position: absolute;
            top: 0;
            left: 0;
        }
    </style>
</head>
<body>
    <div id="app">
        <div id="box" v-Random v-drag >可拖动</div>
    </div>
    <div id="ccb">
        <div :style="obj" v-Random >不可拖拽</div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.js"></script>
    <script type="text/javascript">
        //全局自定义[公有]
        Vue.directive('Random',function(el){
            console.log(el)
            el.onclick = function(event){
                event.preventDefault()
                el.style.backgroundColor = "rgb("+parseInt(Math.random()*256)+","+parseInt(Math.random()*256)+","+parseInt(Math.random()*256)+")" ;
            }
        })
        new Vue({
            el:'#app',
            data () {
                return {
                    w:100,
                    h:100
                }
            },
            // 局部自定义[私有]
            directives: {
                'drag' (el){
                    el.onmousedown=function(e){//鼠标按下
                        var x=e.clientX - el.offsetLeft
                        var y=e.clientY - el.offsetTop
                        
                        document.onmousemove=function(e){//鼠标移动
                            el.style.left=e.clientX - x+"px";
                            el.style.top=e.clientY - y+"px";
                        }
                        
                        document.onmouseup=function(){
                            document.onmousemove=document.onmouseup=null
                        }
				}
                }
            }
        })
        new Vue({
            el:'#ccb',
            data(){
                return{
                    obj:{
                        width: 100+ 'px', 
                        height: 100+ 'px', 
                        backgroundColor:'blue'
                    }
                }
            }
        })
    </script>
</body>
</html>